#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"

void QueueInit(Node* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}
void QueueDestroy(Node* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->tail = pq->head = NULL;
	pq->size = 0;
}
void QueuePushu(Node* pq, QueueDataType x)
{
	assert(pq);
	QNode* newNode = (QNode*)malloc(sizeof(QNode));
	if (newNode == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	newNode->data = x;
	newNode->next = NULL;
	if (pq->tail == NULL)
	{
		pq->tail = pq->head = newNode;
	}
	else
	{
		pq->tail->next = newNode;
		pq->tail = newNode;
	}
	pq->size++;
}
void QueuePop(Node* pq)
{
	assert(pq);
	assert(!QueueEmpt(pq));

	Node* next = pq->head->next;
	free(pq->head);
	pq->head = next;

	if (pq->head == NULL)
	{
		pq->tail = NULL;
	}
	pq->size--;
}
QueueDataType QueueFindFront(Node* pq)
{
	assert(pq);
	assert(!QueueEmpt(pq));

	return pq->head->data;
}
QueueDataType QueueFindBack(Node* pq)
{
	assert(pq);
	assert(!QueueEmpt(pq));

	return pq->tail->data;
}
bool QueueEmpt(Node* pq)
{
	assert(pq);
	
	return pq->head == NULL;
}
int QueueSize(Node* pq)
{
	assert(pq);
	return pq->size;
}
